package com.statistics.export;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.*;


/**
 * @program:
 * @InterfaceName: TaskBookController
 * @description: 任务书管理导出任务书-控制层
 * @Author: han_jun@hoperun.com
 * @Date: 2020/11/18
 */
@RestController
@RequestMapping(value = "/test")
public class TaskBookController {


    private final static Logger logger = LoggerFactory.getLogger(TaskBookController.class);


    /**
     * 任务书管理-合同查询 导出合同模板  word
     *
     * @param response
     * @throws Exception
     */
    @GetMapping(value = "/exportWord")
    public void getContractWord(HttpServletResponse response) throws Exception {

        ExportTaskBookWordDemo exportWord = new ExportTaskBookWordDemo();

        String path = exportWord.getContractWordList("/updateFile/");
        //导出word
        OutputStream ous = null;
        InputStream ins = null;
        File file = new File(path);
        try {
            ins = new BufferedInputStream(new FileInputStream(file));
            byte[] buffer = new byte[ins.available()];
            ins.read(buffer);
            response.reset();
            response.setHeader("Content-Disposition", "attachment; filename=" + file.getName());
            response.addHeader("Content-Length", "" + file.length());
            ous = new BufferedOutputStream(response.getOutputStream());

            response.setContentType("application/msword");

            ous.write(buffer);
            ous.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                ous.close();
                ins.close();
                file.delete();
                //删除文件
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

//    public static void main(String[] args) {
//        ExportTaskBookWordDemo exportWord = new com.example.demo.ExportTaskBookWordDemo();
//
//        String path = exportWord.getContractWordList("/updateFile/");
//        //导出word
//        OutputStream ous = null;
//        InputStream ins = null;
//        File file = new File(path);
//        try {
//            ins = new BufferedInputStream(new FileInputStream(file));
//            byte[] buffer = new byte[ins.available()];
//            ins.read(buffer);
//            ous = new BufferedOutputStream(new FileOutputStream("D:/Java/Project/demo.docx"));
//
//            ous.write(buffer);
//            ous.flush();
//        } catch (IOException e) {
//            e.printStackTrace();
//        } finally {
//            try {
//                ous.close();
//                ins.close();
//                file.delete();
//                //删除文件
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
//    }
}
